﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using CKFinder;

public partial class Admin_EditNews : System.Web.UI.Page
{
    CategoryNewsController categoryController = new CategoryNewsController();
    NewsController newsController = new NewsController();
    protected void Page_Load(object sender, EventArgs e)
    {
        CKFinder.FileBrowser fb = new FileBrowser();
        fb.BasePath = "../ckfinder/";
        fb.SetupCKEditor(ckeditor);
        if (!IsPostBack)
        {
            ddlCategoryNews.DataSource = categoryController.GetAll();
            ddlCategoryNews.DataTextField = "name";
            ddlCategoryNews.DataValueField = "category_news_id";
            ddlCategoryNews.DataBind();
            string id = Request.QueryString["id"];
            if (id == "" || id == null || string.IsNullOrWhiteSpace(id.ToString()) || string.IsNullOrEmpty(id.ToString()))
            {

                error();
            }
            else
            {

                int idd = Convert.ToInt32(id);
                if (!this.checkId(idd))
                {
                    Response.Write(id + "id:");
                    error();
                }
                else
                {
                    DataTable dt = newsController.GetByID(idd);
                    News n = new News(dt.Rows[0]);
                    txtDescription.Text = n.Description;
                    txtTitle.Text = n.Title;
                    txtUrl.Text = n.Url;
                    image_news.ImageUrl = ResolveUrl("~/") + n.Image;
                    ckeditor.Text = n.Content;
                    ddlCategoryNews.Items.FindByValue(n.CategoryNews_id.ToString()).Selected = true;
                    if (n.Status)
                    {
                        ddlStatus.SelectedIndex = 0;
                    }
                    else
                    {
                        ddlStatus.SelectedIndex = 1;
                    }
                }

            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Request.QueryString["id"]);
        NewsController newsController = new NewsController();
        News n = new News();
        n.News_id = id;
        n.Title = txtTitle.Text;
        n.Url = txtUrl.Text;
        n.Description = txtDescription.Text;
        n.Content = ckeditor.Text;
        n.DateStart = DateTime.Now;
        n.CategoryNews_id = Convert.ToInt32(ddlCategoryNews.SelectedValue);
        if (ddlStatus.SelectedIndex == 0)
        {
            n.Status = true;
        }
        else
        {
            n.Status = false;
        }
        n.Image = news_value.Value;
        if (newsController.Update(n) > 0)
        {
            Response.Write("<script>alert('Cập nhật tin tức thành công')</script>");
            
        }
        else
        {
            Response.Write("<script>alert('Cập nhật tin tức thành công')</script>");
            return;
        }
    }
    private static void error()
    {
        HttpContext.Current.Response.Redirect("~/Error.aspx");
    }
    private bool checkId(int id)
    {
        DataTable dt = categoryController.GetByID(id);

        if (dt.Rows.Count > 0)
        {
            return true;
        }
        return false;
    }
}